package com.sheng.leetcode.year2023.month08.day14;

import org.junit.Test;

/**
 * @author by ls
 * @date 2023/8/14
 * <p>
 * 617. 合并二叉树<p>
 * <p>
 * 给你两棵二叉树： root1 和 root2 。<p>
 * 想象一下，当你将其中一棵覆盖到另一棵之上时，两棵树上的一些节点将会重叠（而另一些不会）。<p>
 * 你需要将这两棵树合并成一棵新二叉树。合并的规则是：如果两个节点重叠，<p>
 * 那么将这两个节点的值相加作为合并后节点的新值；否则，不为 null 的节点将直接作为新二叉树的节点。<p>
 * 返回合并后的二叉树。<p>
 * 注意: 合并过程必须从两个树的根节点开始。<p>
 * <p>
 * 示例 1：<p>
 * 输入：root1 = [1,3,2,5], root2 = [2,1,3,null,4,null,7]<p>
 * 输出：[3,4,5,5,4,null,7]<p>
 * <p>
 * 示例 2：<p>
 * 输入：root1 = [1], root2 = [1,2]<p>
 * 输出：[2,2]<p>
 * <p>
 * 提示：<p>
 * 两棵树中的节点数目在范围 [0, 2000] 内<p>
 * -10^4 <= Node.val <= 10^4<p>
 */
public class LeetCode0617 {

    @Test
    public void test01() {
        TreeNode root1 = new TreeNode(1, new TreeNode(3, new TreeNode(5), null), new TreeNode(2));
        TreeNode root2 = new TreeNode(2, new TreeNode(1, null, new TreeNode(4)), new TreeNode(3, null, new TreeNode(7)));
        TreeNode node = new Solution().mergeTrees(root1, root2);
        System.out.println(node);
    }
}

class Solution {
    public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {
        if (root1 != null) {
            if (root2 != null) {
                root1.val = root1.val + root2.val;
                root1.left = mergeTrees(root1.left, root2.left);
                root1.right = mergeTrees(root1.right, root2.right);
            } else {
                mergeTrees(root1.left, null);
                mergeTrees(root1.right, null);
            }
        } else {
            if (root2 != null) {
                root1 = root2;
            }
        }
        return root1;
    }
}


// Definition for a binary tree node.
class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

    TreeNode() {
    }

    TreeNode(int val) {
        this.val = val;
    }

    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}
